package com.cskaoyan.wordmemorize.auth;

import cn.dev33.satoken.stp.StpInterface;
import com.cskaoyan.wordmemorize.dao.mapper.EmployeeMapper;
import com.cskaoyan.wordmemorize.dto.app.UserDTO;
import com.cskaoyan.wordmemorize.service.EmployeeService;
import com.cskaoyan.wordmemorize.service.impl.UserServiceImpl;
import jakarta.annotation.Resource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

// todo StpInterface是 Sa-Token 权限框架中的一个核心接口, 用来获取用户权限和角色的"桥梁"
// todo 根据用户Id和登录类型(User、Admin)获取权限或角色的列表

@Component
public class StpInterfaceImpl implements StpInterface {
    @Autowired
    EmployeeService employeeService;
    @Autowired
    private UserServiceImpl userServiceImpl;

    /*
         只需要自己实现这个方法即可
     */
    @Override
    // todo 登录的时候会获取token Sa-Token通过加密的Token字符串来解析出loginId和loginType
    // todo 登录就是用来获取token的, token则是打开各种请求方法大门的钥匙
    public List<String> getPermissionList(Object loginId, String loginType) {
        if ("admin".equals(loginType)) {
            System.out.println("权限= " + employeeService.getPermissionAlias((Long.parseLong( loginId.toString()))).stream().toList());
            return employeeService.getPermissionAlias((Long.parseLong( loginId.toString()))).stream().toList();
        }
        if ("user".equals(loginType)) {
            return userServiceImpl.getPermissionCode((Long.parseLong( loginId.toString()))).stream().toList();
        }
        return null;
    }

    @Override
    public List<String> getRoleList(Object loginId, String loginType) {
        return null;
    }
}
